package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.dao.OnlineCourseMapper;
import com.lst.entity.request.SportRecordsReqPara;
import com.lst.entity.response.SportRecordsResPara;
import com.lst.model.OnlineCourse;
import com.lst.model.SportRecords;
import com.lst.service.SportRecordsService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: SportRecordsServlet
 * @Description: 运动记录接口
 * 
 */
public class SportRecordServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private SportRecordsService sportRecordsService = ctx.getBean(SportRecordsService.class);
	
	 @Autowired
	private OnlineCourseMapper onlineCourseMapper = ctx.getBean(OnlineCourseMapper.class);
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		logger.info("doPost begin: " + startDate);
		SportRecordsResPara resPara = new SportRecordsResPara();

		try {
			SportRecordsReqPara reqPara = getReqPara(request);
			
		if(isCheckField(reqPara,resPara)){
			Integer userid = Integer.valueOf(reqPara.getUserid());
			
			//统计训练总次数
			Integer totalNum=sportRecordsService.countByUser(userid);
			resPara.setTotalNum(totalNum);
			
			if(totalNum>0){
				//统计总训练时间
				Integer totalTime=sportRecordsService.countByUser2(userid);
				resPara.setTotalTime(totalTime);
				
				//统计总消耗卡路里
				Integer totalCalories=sportRecordsService.countByUser3(userid);
				resPara.setTotalCalories(totalCalories);
				 int trainNum1=0;//平衡与稳定训练数
				 int trainNum2=0;//力量训练次数
				 int trainNum3=0;//灵敏训练次数
				 int trainNum4=0;//反应与速度训练次数
				 int kfNum1=0;//拳法训练数
				 int kfNum2=0;//踢击次数
				 int kfNum3=0;//地面训练次数
				 int typeNum1=0;//基础能力训练数
				 int typeNum2=0;//专项能力训练次数
				 int typeNum3=0;//技术表现训练次数
				 
				List<SportRecords> sportList=sportRecordsService.countByUser4(userid);
				if(sportList!=null&&sportList.size()>0){
					for(SportRecords sportRecords:sportList){
						OnlineCourse onlineCourse=onlineCourseMapper.selectByPrimaryKey(sportRecords.getCourseId());
						if(onlineCourse!=null){
							int trainNum=onlineCourse.getTrainType();
							if(trainNum==CommCode.Train_type1||trainNum==CommCode.Train_type5){
								trainNum1++;
							}
							if(trainNum==CommCode.Train_type2||trainNum==CommCode.Train_type5){
								trainNum2++;
							}
							if(trainNum==CommCode.Train_type3||trainNum==CommCode.Train_type5){
								trainNum3++;
							}
							if(trainNum==CommCode.Train_type4||trainNum==CommCode.Train_type5){
								trainNum4++;
							}
							
							int kfNum=onlineCourse.getKfClassType();
							if(kfNum==CommCode.Kfclass_type1||kfNum==CommCode.Kfclass_type4){
								kfNum1++;
							}
							if(kfNum==CommCode.Kfclass_type2||kfNum==CommCode.Kfclass_type4){
								kfNum2++;
							}
							if(kfNum==CommCode.Kfclass_type3||kfNum==CommCode.Kfclass_type4){
								kfNum3++;
							}
							
							int typeNum=onlineCourse.getReserved1();
							if(typeNum==466){
								typeNum1++;
							}
							if(typeNum==467){
								typeNum2++;
							}
							if(typeNum==468){
								typeNum3++;
							}
							
						}
					}
				}
				
				resPara.setTrainNum1(trainNum1);
				resPara.setTrainNum2(trainNum2);
				resPara.setTrainNum3(trainNum3);
				resPara.setTrainNum4(trainNum4);
				resPara.setKfNum1(kfNum1);
				resPara.setKfNum2(kfNum2);
				resPara.setKfNum3(kfNum3);
				resPara.setTypeNum1(typeNum1);
				resPara.setTypeNum2(typeNum2);
				resPara.setTypeNum3(typeNum3);
				
			}
			
			
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);
			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
			logger.info("SportRecordServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public SportRecordsReqPara getReqPara(HttpServletRequest request) {

		SportRecordsReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new SportRecordsReqPara());

			reqPara = (SportRecordsReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String page = request.getParameter("page");
			String pagesize = request.getParameter("pagesize");
	
			logger.info("reqPara userid:" + userid);
			logger.info("reqPara page:" + page);
			logger.info("reqPara pagesize:" + pagesize);

	        reqPara.setUserid(userid);
	        reqPara.setPage(page);
	        reqPara.setPagesize(pagesize);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		SportRecordsReqPara cdReqPara = (SportRecordsReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

        if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		}  else {
			flag = true;
		}

		return  flag;
	}

}
